package com.hxzy.dao.impl;

import com.hxzy.bean.Discuss;
import com.hxzy.bean.Message;
import com.hxzy.bean.User;
import com.hxzy.dao.DiscussDao;
import com.hxzy.util.CommonUtil;
import com.hxzy.util.JdbcUtils;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author nick
 */
public class DiscussDaoImpl implements DiscussDao {
    @Override
    public int save(Discuss discuss) {
        String sql = "INSERT INTO message_discuss (`content`,`date`,message_id,user_id) VALUES(?,?,?,?)";
        Object[] params = {discuss.getContent(), discuss.getDate(), discuss.getMessage().getId(), discuss.getUser().getId()};
        return JdbcUtils.executeUpdate(sql,params);
    }

    @Override
    public int update(Discuss discuss) {
        return 0;
    }

    @Override
    public int remove(Integer integer) {
        return 0;
    }

    @Override
    public List<Discuss> queryAll() {
        StringBuilder builder = new StringBuilder()
                .append("SELECT ")
                .append(" m.`id` as `id`,")
                .append(" m.`content` as `content`,")
                .append(" m.`date` as `date`,")
                .append(" m.message_id as messageId,")
                .append(" m.user_id as userId,")
                .append(" u.nick_name as nickName")
                .append(" FROM message_discuss m ")
                .append(" INNER JOIN message_users u ")
                .append(" ON u.id = m.user_id ");
        System.out.println(builder);
        List<Map<String, Object>> list = JdbcUtils.executeQuery(builder.toString());
        return list.stream().map(t ->map2Discuss(t)).collect(Collectors.toList());
    }

    private Discuss map2Discuss(Map<String, Object> t) {
        int id = ((Long) t.get("id")).intValue();
        String content = (String) t.get("content");
        Timestamp timestamp = (Timestamp) t.get("date");
        LocalDateTime localDateTime = CommonUtil.timeStamp2LocalDateTime(timestamp);

        int messageId = ((Long) t.get("messageId")).intValue();

        int userId = ((Long) t.get("userId")).intValue();
        String nickName = (String) t.get("nickName");
        String pic = (String) t.get("pic");

        Message message = new Message();
        message.setId(messageId);

        User user = new User();
        user.setId(userId);
        user.setNickName(nickName);
        user.setPic(pic);

        return new Discuss(id, content, localDateTime, message, user);
    }

    @Override
    public Discuss findById(Integer integer) {
        StringBuilder builder = new StringBuilder()
                .append("SELECT ")
                .append(" m.`id` as `id`,")
                .append(" m.`content` as `content`,")
                .append(" m.`date` as `date`,")
                .append(" m.message_id as messageId,")
                .append(" m.user_id as userId,")
                .append(" u.nick_name as nickName,")
                .append(" u.pic as pic")
                .append(" FROM message_discuss m ")
                .append(" INNER JOIN message_users u ")
                .append(" ON u.id = m.user_id ")
                .append(" WHERE m.id = ? ");
        List<Map<String, Object>> list = JdbcUtils.executeQuery(builder.toString(),integer);
        Optional<Discuss> first = list.stream().map(t -> map2Discuss(t)).findFirst();
        return first.orElse(null);
    }

    @Override
    public List<Discuss> paging(int pageIndex, int pageSize, int messageId) {
        StringBuilder builder = new StringBuilder()
                .append("SELECT ")
                .append(" m.`id` as `id`,")
                .append(" m.`content` as `content`,")
                .append(" m.`date` as `date`,")
                .append(" m.message_id as messageId,")
                .append(" m.user_id as userId,")
                .append(" u.nick_name as nickName,")
                .append(" u.pic as pic")
                .append(" FROM message_discuss m ")
                .append(" INNER JOIN message_users u ")
                .append(" ON u.id = m.user_id ")
                .append(" WHERE m.message_id = ? ")
                .append(" ORDER BY m.`date` desc")
                .append(" limit ?,?");
         Object[] params = {
                 messageId,
                 (pageIndex - 1) * pageSize,
                 pageSize
         };
        List<Map<String, Object>> list = JdbcUtils.executeQuery(builder.toString(),params);
        return list.stream().map(this::map2Discuss).collect(Collectors.toList());
    }

    @Override
    public int totals(int messageId) {
        StringBuilder builder = new StringBuilder()
                .append("SELECT ")
                .append(" count(0) as total")
                .append(" FROM message_discuss m ")
                .append(" INNER JOIN message_users u ")
                .append(" ON u.id = m.user_id ")
                .append(" WHERE m.message_id = ? ");
        Map<String, Object> map = JdbcUtils.executeQuery(builder.toString(),messageId).get(0);
        Long l = (Long) map.get("total");
        return l.intValue();
    }
}
